package com.milkTeaServer.dao;

import com.milkTeaServer.bean.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Transactional
@Repository
public interface UserDao extends JpaRepository<User, Integer> {
    @Modifying
    @Query(value = "update t_user set password = :password where id = :id", nativeQuery = true)
    int updatePasswordById(@Param("password") String password, @Param("id") Integer id);

    @Modifying
    @Query(value = "update t_user set email = :email where id = :id", nativeQuery = true)
    int updateEmailById(@Param("email") String email, @Param("id") Integer id);

    @Modifying
    @Query(value = "update t_user set phone = :phone where id = :id", nativeQuery = true)
    int updatePhoneById(@Param("phone") String phone, @Param("id") Integer id);

    @Modifying
    @Query(value = "update t_user set sex = :sex where id = :id", nativeQuery = true)
    int updateSexById(@Param("sex") String sex, @Param("id") Integer id);

    @Modifying
    @Query(value = "update t_user set money = :money where id = :id", nativeQuery = true)
    int recharge(@Param("money") float money,@Param("id") Integer id);

    @Modifying
    @Query(value = "update t_user set money = :money where id = :id", nativeQuery = true)
    int idByMoney(@Param("money")float money,@Param("id") Integer id);

    @Modifying
    @Query(value="select id,name, password, phone,email,sex,img,money from t_user where id = ?1 order by id desc",
            nativeQuery=true )
    List<User> findUserById(Integer id);

    @Modifying
    @Query(value = "select id,name, password, phone,email,sex,img,money from t_user where name = ?1 and password = ?2 order by id desc", nativeQuery = true)
    List<User> selectUser(@Param("name") String name, @Param("password") String password);
}

